package com.xiangxiao.rpan.storage.core;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/7/31 17:09
 */
@Component(value = "storageManager")
public class StorageManager {
  @Autowired
  @Qualifier(value = "storageProcessorSelector")
  private StorageProcessorSelector storageProcessorSelector;

  /**
   * 文件存储
   *
   * @param file 输入文件
   * @param suffix  文件名后缀
   * @return 文件存储路径
   * @throws IOException
   */
  public String store(MultipartFile file, String suffix) throws IOException {
    return storageProcessorSelector.select().store(file, suffix);
  }

  /**
   * 文件分片存储
   *
   * @param file 输入文件
   * @param identifier  文件唯一标识
   * @param totalChunks 分片总数
   * @param chunkNumber 当前分片下标 从1开始
   * @param totalSize   文件总大小
   * @param chunkSize   分片文件大小
   * @return 文件存储路径
   * @throws IOException
   */
  public String storeWithChunk(MultipartFile file, String identifier, Integer totalChunks, Integer chunkNumber, Long totalSize, Long chunkSize, String redisFileDataKey) throws IOException {
    return storageProcessorSelector.select().storeWithChunk(file, identifier, totalChunks, chunkNumber, totalSize, chunkSize, redisFileDataKey);
  }

  /***
   * 合并各文件分片
   * md5  文件md5标识
   * chunkCount 分片片数
   * */
  public String mergeChunks(String md5, int chunkCount, String suffix) throws IOException {
    return storageProcessorSelector.select().mergeChunks(md5, chunkCount, suffix);
  }

  /**
   * 文件下载
   * @param path 文件绝对路径
   * @param response  返回体response
   * @throws IOException
   */
  public String download(String path, HttpServletResponse response) throws IOException {
    return storageProcessorSelector.select().download(path, response);
  }

  /**
   * 文件分片
   * @param bytes 流
   * @param response  返回体response
   * @throws IOException
   */
  public String downloadWithChunk(byte[] bytes, HttpServletResponse response) throws IOException {
    return storageProcessorSelector.select().downloadWithChunk(bytes, response);
  }



}
